package com.neusoft.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author: duan
 * @Date: Create in 15:38 2020/11/3
 */
public class CharactorEncodingInterceptor implements HandlerInterceptor {

    @Override
    /**
     *  preHandle
     *      拦截一触发，就会进到该方法，由方法的返回值确定是否放行
     *          false：不通过
     *          true：通过
     *      HttpServletRequest request：请求
     *      HttpServletResponse response：响应
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("CharactorEncodingInterceptor:::preHandle");
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("CharactorEncodingInterceptor:::postHandle");
        modelAndView.addObject("msg", "postHandle这是拦截器添加的内容");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("CharactorEncodingInterceptor:::afterCompletion");
    }
}
